import sentry_sdk
from fastapi import FastAPI
from fastapi.routing import APIRoute
from loguru import logger

from app.api.routes import api_router
from app.core.config import settings
from app.core.log import log_init
from app.core.middlewares import RequestLogMiddleware


def custom_generate_unique_id(route: APIRoute) -> str:
    return f"{route.tags[0]}-{route.name}"


log_init()
logger.info(f"Server initialization start, settings={settings.dict()}")
if settings.SENTRY_DSN:
    sentry_sdk.init(dsn=str(settings.SENTRY_DSN), enable_tracing=True)
    logger.info("Sentry enabled")

app = FastAPI(
    title=settings.PROJECT_NAME,
    openapi_url=f"{settings.API_V1_STR}/openapi.json",
    generate_unique_id_function=custom_generate_unique_id,
)
app.include_router(api_router, prefix=settings.API_V1_STR)
app.add_middleware(RequestLogMiddleware)
logger.info("Server initialized")
